package com.df.springmvc.system.auth.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.util.SN_GUID;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.system.auth.dao.AuthDao;

/**
 * 本类只做数据运算处理
 * 
 * @author David
 */
@Service
public class AuthServices extends SupperServices {
	@Autowired
	AuthDao dao;

	public List<Object> findAllEmployees() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据角色编号获取权限
	 * @param roleCode
	 * @return
	 * @throws Exception
	 */
	public Object add(HashMap<String,String> map) throws Exception{
		String authCode = map.get("authCode");
		String roleCode = map.get("roleCode");
		String systemCode = map.get("systemCode");
		ArrayList<String[]> list = new ArrayList<String[]>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if(!"".equals(map.get("authCode"))){
			String[] spAc = authCode.split(",");
			for (int i = 0; i < spAc.length; i++) {
				String[] ss = new String[7];
				ss[0]=SN_GUID.getUUID();//guid
				ss[1]="1";//status
				ss[2]=getCurrentUserBean().getUserId();//createBy
				ss[3]=sdf.format(new Date());//createTime
				ss[4] = spAc[i];
				ss[5] = roleCode;
				ss[6] = systemCode;
				list.add(ss);
			}
		}
		
		Object o = dao.addRoleAuth(map,list);
		if(Integer.valueOf(o.toString())>0){
			return "配置成功！";
		}else{
			return "配置失败！";
		}
	}
	
	
	
	/**
	 * 根据系统获取权限列表
	 * @return
	 * @throws Exception
	 */
	public  Object getAuthDate(String systemCode) throws Exception {
		// TODO Auto-generated method stub
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		ArrayList<HashMap<String, Object>> listModular = (ArrayList<HashMap<String,Object>>) dao.getModularCode(systemCode);
		for (HashMap<String, Object> map : listModular) {
			 Map<String,Object> m =new HashMap<String,Object>();
			String modularCode = (String) map.get("modularCode");
			m.put("modularCode", (String) map.get("modularCode"));
			m.put("modularName", (String) map.get("modularName"));
			List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>();
			ArrayList<HashMap<String, Object>> listFunction = (ArrayList<HashMap<String,Object>>) dao.getFunctionCode(modularCode);
				for (HashMap<String, Object> map2 : listFunction) {
					Map<String,Object> m2 =new HashMap<String,Object>();
					String functionCode = (String) map2.get("functionCode");
			        m2.put("functionCode", map2.get("functionCode"));
			        m2.put("functionName", map2.get("functionName"));
			        
			        List<Map<String,Object>> list3 = new ArrayList<Map<String,Object>>();
					
					ArrayList<HashMap<String, Object>> listOperation = (ArrayList<HashMap<String,Object>>) dao.getOperationCode(functionCode);
					for (HashMap<String, Object> map3 : listOperation) {
						Map<String,Object> m3 =new HashMap<String,Object>();
						if("view".equals(map3.get("operationCode"))){
							
							m2.put("authCode", map3.get("authCode"));
							m2.put("viewCode", map3.get("operationCode"));
							m2.put("viewName", map3.get("operationName"));
						}else{
							m3.put("authCode", map3.get("authCode"));
							m3.put("operationCode", map3.get("operationCode"));
					        m3.put("operationName", map3.get("operationName"));
				        list3.add(m3);
						}
					}
					 m2.put("operation", list3);
					 list2.add(m2);
				}
				m.put("functionM", list2);
				list.add(m);
		}
		
		return list;
	}
	/**
	 * 获取角色
	 * @return
	 * @throws Exception
	 */
	public Object getRoleDate() throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = (ArrayList<HashMap<String,Object>>) dao.getRole();
		return list;
	}
	/**
	 * 获取系统列表
	 * @return
	 * @throws Exception
	 */
	public ArrayList<HashMap<String, Object>> getGetSystem() throws Exception{
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getGetSystem();
		return list;
	}
	/**
	 * 根据角色编号获取权限
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Object getAuthCode(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getAuthCode(map);
		return list;
	}

	/**
	 * 根据角色编号获取权限（权限控制使用，公用方法）
	 * @param roleCode
	 * @return
	 * @throws Exception
	 */
	public ArrayList<HashMap<String, Object>> getAuthUseRoleCode(String roleCode) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getAuthUseRoleCode(roleCode);
		return list;
	}
}
